NXT Counter—an NXC Tutorial
A long time ago, when I was a college student, I had a summer job working for my hometown's park authority. As a temporary summer employee I had no fixed job, but was given whatever odd jobs that came up that nobody else wanted. One day I was handed a mechanical counter and told to stand at the park's main entrance and count people as they entered the park. The counter was a mechanical device with a display that looked like a automobile odometer. It had three keys much like the keys on a mechanical typewriter. Pushing down on one of the keys increased the count by one, pushing the second key decreased the count by one (intended for correcting mistakes), and the third key reset the counter to zero. I spent one of most boring days of life with that counter. I don't remember exactly what the count was at the end of the day, but it was in the thousands.
     That day in the park is the inspiration behind the NXT counter program. It may not be a program with much real utility, but it makes a good tutorial. It is neither too long nor too short. It covers an approach to multitasking that is both safe and applicable to many situations. And it has the advantage of requiring no other LEGO parts other than the NXT itself.
The program demonstrates many features of NXC, but my main purpose for making it available is twofold:
To show how several tasks can safely share a common resource under the protection of a mutex.
To showcase the divide-and-conquer approach to program development. You will see how generating a seven-segment numerical display can be broken down into a collection of very simple functions.
Acknowlegement
In his books on programming, author Dan Shafer always liked to present a simulation of a counter much like the one I described above as his first example of a program above the level of hello-world. I believe Dan had a good idea, so when I decided to a tutorial on NXC programming, I based it on his idea as well as my memory of that day in the park.


Morton Goldberg
Ann Arbor, Michigan
October, 2010